package com.java.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.exception.BusinessExceptionEnum;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.common.utils.R;
import com.java.mapper.UsersDao;
import com.java.pojo.Users;
import com.java.pojo.bo.UserBo;
import com.java.service.UsersService;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Users> page = this.page(
                new Query<Users>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Users>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean queryUsernameIsExist(String username) {
        Users users = this.baseMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUsername, username));
        return users != null;
    }

    @Override
    public Users createUser(UserBo userBo) {
        String hashpw = BCrypt.hashpw(userBo.getPassword(), BCrypt.gensalt());
        Users users = new Users();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        long id = snowflake.nextId();
        users.setId(String.valueOf(id));
        users.setUsername(userBo.getUsername());
        users.setPassword(hashpw);
        // 默认昵称同用户名
        users.setNickname(userBo.getUsername());
        // 默认生日 1900-01-01
        users.setBirthday(DateUtil.parse("1900-01-01"));
        this.baseMapper.insert(users);
        return users;
    }



    @Override
    public Users login(String username, String password) {

        return null;
    }
}